package com.leetcode.demo;

import java.util.Arrays;

/**
 * @Author: WuPeng
 * @Date: 2021/7/6 6:08 下午
 */
public class Solution {

    public double findMedianSortedArrays(int[] nums1, int[] nums2) {
        int length = nums1.length + nums2.length;
        int[] new_arr = new int[length];
        int idx1=0,idx2=0,idx=0;
        while(idx1 < nums1.length || idx2 < nums2.length){
            if(idx2 < nums2.length && idx1 >= nums1.length){
                new_arr[idx++] = nums2[idx2++];
            }
            else if(idx1 < nums1.length && idx2 >= nums2.length){
                new_arr[idx++] = nums1[idx1++];
            }
            else if(nums1[idx1] <= nums2[idx2]){
                new_arr[idx++] = nums1[idx1++];
            }
            else if(nums1[idx1] > nums2[idx2]){
                new_arr[idx++] = nums2[idx2++];
            }
        }
        System.out.println(Arrays.toString(new_arr));
        if((length%2) == 0){
            return (new_arr[length/2] + new_arr[length/2-1]) / 2.0;
        } else{
            return new_arr[length/2] * 1.0;
        }
    }

    public static void main(String[] args) {
        Solution solution = new Solution();
        int[] nums1 = {1,2};
        int[] nums2 = {3,4};
        System.out.println(solution.findMedianSortedArrays(nums1, nums2));
    }

}
